# coding:utf-8

# 英文情感分析
from jieba import analyse
from snownlp import SnowNLP
from textblob import TextBlob

from sentiment.utils import LanguageUtil


class BeiyesSentiment(object):
    @staticmethod
    def english_analyse(sentence):
        text = sentence
        blob = TextBlob(text)

        # 拆分句子
        blob.sentences  # [Sentence("I am happy today."), Sentence("I feel sad today.")]

        # polarity代表情感极性，取值范围是[-1, 1]，-1代表完全负面，1代表完全正面
        # subjectivity代表主观性程度
        # blob.sentences[0].sentiment  # Sentiment(polarity=0.8, subjectivity=1.0)
        # blob.sentences[1].sentiment  # Sentiment(polarity=-0.5, subjectivity=1.0)

        # 对整段话进行情感分析
        blob.sentiment  # Sentiment(polarity=0.15000000000000002, subjectivity=1.0)
        # if blob.sentiment.polarity>=0:
        #     return 1
        # else:
        #     return -1
        return blob.sentiment.polarity

    # 中文情感分析
    @staticmethod
    def chinese_analyse(sentence):
        s = SnowNLP(sentence)
        print('中文情感分析:',sentence, s.sentiments)
        return s.sentiments
        # if s.sentiments>0.6:
        #     return 1
        # else:
        #     return -1
    # 情感分析
    @staticmethod
    def sentiment(sentence):
        if LanguageUtil.check_contain_chinese(sentence):
            return BeiyesSentiment.chinese_analyse(sentence)
        else:
            return BeiyesSentiment.english_analyse(sentence)

    @staticmethod
    def chinese_keyword(text, n):
        keywords = analyse.extract_tags(text, topK=n, allowPOS=('ns'))
        return keywords

# def english_keyword(text, n):
#     s = SnowNLP(text)
#     return s.keywords(n)


if __name__ == '__main__':
    # result = BeiyesSentiment.chinese_keyword('Python is a high-level, general-purpose programming language.', 5)
    # print(result)
    res = BeiyesSentiment.sentiment('垃圾商品，垃圾，垃圾')
    print(res)
